﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
// Date: 24/01/2009
// Time: 14:25
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: ${FILE_NAME}.cs
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;

namespace csharp.personal.rts
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : KryptonForm
	{
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
		}
		
		private void showForm(KryptonForm form)
		{
			try 
			{
				form.ShowDialog();
			} 
			finally
			{
				form.Dispose();
			}
		}
		
		
		private void TsmiRegisterUserClick(object sender, EventArgs e)
		{
			entries.frmRegisterUser form = new entries.frmRegisterUser();
			
			showForm(form);
		}
		
		void MainFormLoad(object sender, EventArgs e)
		{
			/*common.Database.Protocol = common.Database.MYSQL;
			common.Database.Host		 = "localhost";
			common.Database.User     = "root";
			common.Database.Password = "";
			common.Database.Name		 = "rts";
			common.Database.Connect();*/

			common.Database.Protocol = common.Database.SQLITE;
			common.Database.Name = "rts.db";
			common.Database.Password = "evolution";			
			if (!System.IO.File.Exists("rts.db")) 
				makeDb();  
			
			common.Database.Connect();
			
			if (!common.Database.IsConnected)
			{
				MessageBox.Show("Banco de dados não conectado!\n"+common.Database.result.ErrorMessage);
				Application.Exit();
			}
		}
		
		void TsmiRegisterDemarcationClick(object sender, EventArgs e)
		{
			frmRegisterDemarcation form = new frmRegisterDemarcation();
			showForm(form);
		}
		
		void TsmiGraphMarkClick(object sender, EventArgs e)
		{
			frmGraficoProducao form = new frmGraficoProducao();
			showForm(form);
		}
		
		void Button1Click(object sender, EventArgs e)
		{
			frmCalendar form = new frmCalendar();
			showForm(form);
		}
		
		private void makeDb()
		{
			String sql;
			
			common.Database.createDatabase(false);
			
			common.Database.Connect();
			
			//common.Database.getSqliteConnection().SetPassword(common.Database.Password);
			
			sql = "CREATE TABLE user ("
				  +" [user_id] INTEGER PRIMARY KEY AUTOINCREMENT, " 
				  +" [user_real_name] NVARCHAR(100), "
				  +" [user_name] NVARCHAR(50), " 
				  +" [user_pass] NVARCHAR(50), "
				  +" [user_email] NVARCHAR(100), "
				  +" [user_create_date] DATE DEFAULT CURRENT_DATE, "
				  +" [user_active] INTEGER "
				  +")";
			
			common.Database.ExecuteQuery(sql);
			
			sql = "CREATE TABLE demarcation ("
				  +" [demarcation_id] INTEGER PRIMARY KEY AUTOINCREMENT, " 
				  +" [demarcation_user_id] INTEGER, "
				  +" [demarcation_date] DATE, "				
				  +" [demarcation_create_date] DATE, "
				  +" [demarcation_place] NVARCHAR(200), "	
				  +" [demarcation_meters] INTEGER, "
				  +" [demarcation_time] TIME, "
				  +" [demarcation_speed] REAL, "
				  +" [demarcation_mark] NVARCHAR(50), "
				  +" [demarcation_fc] NVARCHAR(20), "
				  +" [demarcation_obs] TEXT "
				  +")";
			
			common.Database.ExecuteQuery(sql);	
			
			sql = "CREATE TABLE sport ("
				+" [sport_id] INTEGER PRIMARY KEY AUTOINCREMENT, "
				+" [sport_name] NVARCHAR(100) "
				+")";
			
			common.Database.ExecuteQuery(sql);
			
			sql= " INSERT INTO sport (sport_name) VALUES('natacao'); "
				 +" INSERT INTO sport (sport_name) VALUES('ciclismo'); "
				 +" INSERT INTO sport (sport_name) VALUES('corrida'); ";

			common.Database.ExecuteQuery(sql);
			//common.Database.Disconnect();
		}
	}
}
